<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Think\Controller;
/**
 * 满送活动
 * @author Scor 2015/09/30
 *
 */
class PacketController extends Controller {
      public function index(){
          $currentHour=date("G",time());
          if($currentHour<10){
              $currentDay=date("Y-m-d",time());
              $currentDayReduce=date("Y-m-d",time()-3600*24);
          }else if($currentHour>13){
              $currentDay=date("Y-m-d",time()+24*3600);
              $currentDayReduce=date("Y-m-d",time());
          }else{
              $this->display();
              return false;
          }

//          dump($currentDay);
          $sql="select a.*  from gr_v_order a where (a.report_day1 = '".$currentDay."' and a.report_day2 < a.report_day1) or (a.report_day1 = '".$currentDay."' and a.report_day1 = a.report_day2 and ms <= 0630) order by a.createdate asc";
          $lists = M("order")->query($sql);
          for($i=0;$i<count($lists);$i++){
              $buyer=M('buyers')->where(array("id"=>$lists[$i]["uid"]))->find();
              $lists[$i]["buyerName"]=$buyer['realName'];
              $orderList[]=$lists[$i]["orderid"];
          }
          $map2["orderid"]=array ('in',$orderList);
          $v_goods=M("vorder_goods")->where($map2)->field("pname,sum(cartnum) as sum")->group("pname")->order("sum(cartnum) desc")->select();
          $this->assign("lists",$lists);
          $this->msg=$currentDayReduce."十三点至".($currentDay)."六点半";
          $this->assign("goodslists",$v_goods);
          $this->display();
      }
        public function detail($orderid = null) {
        $map['orderid'] = $orderid;
        $orderlist = D('Order')->where($map)->select();
        $order = $orderlist[0];
        $order['order_goods'] =D('oderder')->query("select b.name pname,b.quantity,b.price pprice,a.* from gr_order_goods a,gr_product b where  a.pid = b.id and a.orderid = '".$map['orderid']."'");
    //        var_dump($order['order_goods']);
        $this->order = $order;
        $this->display();
        }
    public function packetIt($orderid = null) {
        $map['orderid'] = $orderid;
        $res=M("order")->where($map)->find();
        if($res['is_packet']==1){
            $this->error("注意，该订单已被打包");
        }
        $data['is_packet']=1;
        if(D('Order')->where($map)->save($data)){
            $this->success("申请成功");
        }else{
            $this->error("系统错误，请联系技术支持");
        }

    }
}
